package com.wqt;

import java.util.Comparator;
import java.util.TreeSet;

public class TreeDemo {
    public static void main(String[] args) {
        test0();
//        test1();
        test2();
        System.out.println(sum(10,20,30));
    }
    public static void test0(){
        TreeSet<Integer> set1 = new TreeSet<>();
        set1.add(1);
        set1.add(2);
        set1.add(3);
        set1.add(4);
        System.out.println(set1);
        for (Integer i : set1){
            System.out.println(i);
        }
    }
//    public static void test1(){
//        TreeSet<Student> set2 = new TreeSet<>();
//        set2.add(new Student("zhangsan",19));
//        set2.add(new Student("Tom",18));
//        set2.add(new Student("Jack",17));
//        for (Student s : set2){
//            System.out.println(s.getName()+"    "+s.getAge());
//        }
//    }

    public static void test2(){
        Comparator<Student> comparator = (o1, o2) -> o1.getAge()-o2.getAge();
        TreeSet<Student> set3 = new TreeSet<>(comparator);
        set3.add(new Student("zhangsan",19));
        set3.add(new Student("Tom",18));
        set3.add(new Student("Jack",17));
        for(Student s :set3){
            System.out.println(s.getName()+"    "+s.getAge());
        }
    }

    public static int sum(int... a){
        int total=0;
        for (int i=0;i<a.length;i++){
            total+=a[i];
        }
        return total;
    }
}